`

Listing 1-19

Accepting user input with bash

Save and run this script as input_prompting.sh. Notice that you

get prompted to enter information that then get printed:

$ chmod u+x input_prompting.sh

$./input_prompting

What is your first name?

John

What is your last name?

Doe

Your first name is John and your last name is Doe

This script is available at https://github.com/dolevf/Black-Hat-

Bash/blob/master/ch01/input_prompting.sh.

Exit Status Codes

Bash commands return status codes, which indicate whether the

execution of the command succeeded. Status codes fall in the 0255

range, where 0 means success, 1 means failure, 126 means that the

command was found but is not executable, and 127 means the

command was not found. The meaning of any other number depends

on the specific command being used and the logic it uses.

Checking Status Codes

To see status codes in action, save the following script to a file

named exit_codes.sh and run it.

#!/bin/bash

# Experimenting with status codes

ls -l > /dev/null

echo "The status code of the ls command was: $?"

lzl 2> /dev/null

echo "The status code of the non-existing lzl command was: $?"

We use the special variable $? with the echo command to

return the status codes of the executed commands ls and lzl. We

also redirect their standard output and standard error streams to the

file /dev/null, a special device file that discards any data sent to it.

When you want to silence commands, you can redirect their output

to it.

Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks